package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.ReportDTO;
import com.lastproject.ejb.entities.Employee;
import com.lastproject.ejb.entities.Report;

@Stateless
@LocalBean
public class ReportConversion {

	@PersistenceContext
	EntityManager em;

	public ReportDTO fromEntity(Report report) {
		ReportDTO reportDTO = new ReportDTO();

		reportDTO.setId(report.getId());

		reportDTO.setName(report.getName());

		reportDTO.setSelClause(report.getSelClause());

		reportDTO.setWhereClause(report.getWhereClause());
		
		reportDTO.setFromClause(report.getFromClause());
		
		reportDTO.setSortClause(report.getSortClause());

		reportDTO.setDate(report.getDate());

        reportDTO.setIdEmployee(report.getEmployee().getId());

        reportDTO.setConfidential(report.isConfidential());



		return reportDTO;
	}

	public Report fromDTO(ReportDTO reportDTO) {

		Report report = em.find(Report.class, reportDTO.getId());

		if (report == null) {

			report = new Report();

		}

		report.setId(reportDTO.getId());

		report.setDate(reportDTO.getDate());

		report.setName(reportDTO.getName());

		report.setSelClause(reportDTO.getSelClause());

		report.setWhereClause(reportDTO.getWhereClause());
		
		report.setFromClause(reportDTO.getFromClause());
		
		report.setSortClause(reportDTO.getSortClause());
		
		report.setConfidential(reportDTO.isConfidential());
		
		report.setEmployee(em.find(Employee.class, reportDTO.getIdEmployee()));

		return report;

	}

}
